home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Beginning Mac Programming
/
Beginning Mac Programming.bin
/
Open Me for REALbasic 3
/
REALbasic 3.2
/
Example Projects
/
Techniques
/
String Functions
/
string functions source
< prev
next >
Wrap
Text File
|
1998-02-24
|
22KB
|
901 lines
Window1.parseButton.Action:
Sub Action()
output.text = csReplaceAll (input.text, "blUrk", "I SAID BLURK")
End Sub
StringFunctions.isSpace:
Function isSpace(query as string) As Boolean
if query = "" then
return false
end if
if (left (query, 1)) = (chr (13)) or (left (query, 1)) = (chr (32)) or (left (query, 1)) = (chr (9)) then
return true
else
return false
end if
End Function
StringFunctions.nibbleRight:
Function nibbleRight(entry as string) As String
dim tempString as string
dim tempInt as integer
tempString = entry
tempInt = len (entry)
do
if tempString = "" then
return tempString
end if
if isSpace (mid (tempString, tempInt)) then
tempInt = tempInt - 1
tempString = left (tempString, tempInt)
else
return tempString
end if
loop
End Function
StringFunctions.isChar:
Function isChar(query as string) As Boolean
if query = "" then
return false
end if
if ( ( ((asc (query)) > 47) and ((asc (query)) < 58) ) or ( ((asc (query)) > 64) and ((asc (query)) < 91) ) or ( ((asc (query)) > 96) and ((asc (query)) < 123) ) ) then
return true
else
return false
end if
End Function
StringFunctions.isPunct:
Function isPunct(query as string) As boolean
dim tempInt as integer
if query = "" then
return false
end if
tempInt = asc (query)
if tempInt = 33 or tempInt = 34 or tempInt = 39 or tempInt = 44 or tempInt = 46 or tempInt = 58 or tempInt = 59 or tempInt = 63 then
return true
else
return false
end if
End Function
StringFunctions.isLetter:
Function isLetter(query as string) As boolean
dim tempInt as integer
if query = "" then
return false
end if
tempInt = asc (query)
if ( ( (tempInt > 64) and (tempInt < 91) ) or ( (tempInt > 96) and (tempInt < 123) ) ) then
return true
else
return false
end if
End Function
StringFunctions.nibbleLeft:
Function nibbleLeft(entry as string) As String
dim tempString as string
tempString = entry
do
if tempString = "" then
return tempString
end if
if isSpace (left (tempString, 1)) then
tempString = mid (tempString, 2)
else
return tempString
end if
loop
End Function
StringFunctions.nibble:
Function nibble(entry as string) As String
dim tempString as string
tempString = nibbleRight (entry)
tempString = nibbleLeft (tempString)
return tempString
End Function
StringFunctions.chewRight:
Function chewRight(entry as string) As String
dim tempString as string
dim tempInt as integer
tempString = entry
tempInt = len (entry)
do
if tempString = "" then
return tempString
end if
if isSpace (mid (tempString, tempInt)) or isPunct (mid (tempString, tempInt)) then
tempInt = tempInt - 1
tempString = left (tempString, tempInt)
else
return tempString
end if
loop
End Function
StringFunctions.chewLeft:
Function chewLeft(entry as string) As String
dim tempString as string
tempString = entry
do
if tempString = "" then
return tempString
end if
if isSpace (left (tempString, 1)) or isPunct (left (tempString, 1)) then
tempString = mid (tempString, 2)
else
return tempString
end if
loop
End Function
StringFunctions.chew:
Function chew(entry as string) As String
dim tempString as string
tempString = chewLeft (entry)
tempString = chewRight (tempString)
return tempString
End Function
StringFunctions.chompRight:
Function chompRight(entry as string) As String
dim tempString as string
dim tempInt as integer
tempString = entry
tempInt = len (entry)
do
if tempString = "" then
return tempString
end if
if isChar (mid (tempString, tempInt)) then
return tempString
else
tempInt = tempInt - 1
tempString = left (tempString, tempInt)
end if
loop
End Function
StringFunctions.chompLeft:
Function chompLeft(entry as string) As String
dim tempString as string
tempString = entry
do
if tempString = "" then
return tempString
end if
if isChar (left (tempString, 1)) then
return tempString
else
tempString = mid (tempString, 2)
end if
loop
End Function
StringFunctions.chomp:
Function chomp(entry as string) As String
dim tempString as string
tempString = chompLeft (entry)
tempString = chompRight (tempString)
return tempString
End Function
StringFunctions.getBracketedText:
Function getBracketedText(leftBracket as string, rightBracket as string, instance as integer, entry as string) As String
dim loopCounter as integer
dim tempInt1 as integer
dim tempInt2 as integer
dim tempString1 as string
dim holdingString as string
if leftBracket = "" or rightBracket = "" or instance < 1 or entry = "" then
return ""
end if
tempString1 = entry
loopCounter = 0
if leftBracket = rightBracket then
do
tempInt1 = inStr (tempString1, rightBracket)
tempInt2 = inStr (tempInt1 + 1, tempString1, rightBracket)
if tempInt2 = 0 then
return "" // get out of here if you can't find any instances of the bracket
end if
if tempInt2 = (tempInt1 +1) then // they're right on top of each other, get rid of the first
loopCounter = loopCounter -1
tempString1 = mid (tempString1, tempInt2)
else // there's something there, but is it just a space?
holdingString = mid (tempString1, (tempInt1+1), (tempInt2 - tempInt1 - 1))
if hasNonSpace (holdingString) then // we're okay
tempString1 = mid (tempString1, tempInt2 )
else
tempString1 = mid (tempString1, tempInt2)
loopCounter = loopCounter - 1
end if
end if
loopCounter = loopCounter + 1
loop until loopCounter = instance
return holdingString
else // different brackets!
do
tempInt1 = inStr (tempString1, rightBracket)
if tempInt1 = 0 then
return "" // get out of here if you can't find any instances of the bracket
end if
if tempInt1 < 2 then // ignore if it's the first thing there
tempString1 = mid (tempString1, (tempInt1 + 1))
end if
holdingString = left (tempString1, (tempInt1-1))
if inStr (holdingString, leftBracket) = 0 then // see if leftBracket's there
// if not, chop down the string and don't increment the loop
tempString1 = mid (tempString1, (tempInt1 + 1))
loopCounter = loopCounter - 1
else
holdingString = chompToFromLeft (holdingString, leftBracket)
if hasNonSpace (holdingString) then
tempString1 = mid (tempString1, (tempInt1 + 1))
else // the remaining bit is all spaces, so ignore
tempString1 = mid (tempString1, (tempInt1 + 1))
loopCounter = loopCounter - 1
end if
end if
loopCounter = loopCounter + 1
loop until loopCounter = instance
return holdingString
end if
End Function
StringFunctions.hasNonSpace:
Function hasNonSpace(entry as string) As Boolean
dim loopCounter as integer
if entry = "" then
return false
end if
for loopCounter = 1 to len (entry)
if isSpace (mid (entry, loopCounter, 1)) then
// do nothing
else
return true
end if
next
return false
End Function
StringFunctions.chompToFromLeft:
Function chompToFromLeft(entry as string, target as string) As String
dim tempInt as integer
dim tempString as string
if entry = "" or target = ""then
return ""
end if
tempString = entry
do
tempInt = inStr (tempString, target)
if tempInt = 0 then
exit
else
tempString = mid (tempString, (tempInt + len (target)))
end if
loop
return tempString
End Function
StringFunctions.chompToFromRight:
Function chompToFromRight(entry as string, target as string) As String
dim tempString as string
dim tempInt as integer
if entry = "" or target = "" then
return ""
end if
tempInt = inStr (entry, target)
if tempInt = 0 then
return entry
else
tempString = left (entry, (tempInt - 1))
return tempString
end if
return tempString
End Function
StringFunctions.nibbleToFromLeft:
Function nibbleToFromLeft(entry as string, target as string) As string
dim tempString as string
dim tempInt as integer
if entry = "" or target = "" then
return ""
end if
tempInt = inStr (entry, target)
if tempInt = 0 then
return entry
else
tempString = mid (entry, (tempInt + len (target)))
return tempString
end if
End Function
StringFunctions.nibbleToFromRight:
Function nibbleToFromRight(entry as string, target as string) As string
dim holdingInt as integer
dim tempInt as integer
dim tempString as string
if entry = "" or target = "" then
return ""
end if
do
tempInt = inStr ((holdingInt + 1), entry, target)
if tempInt = 0 then
exit
else
holdingInt = tempInt
end if
loop
tempString = left (entry, (holdingInt - 1))
return tempString
End Function
StringFunctions.chewToFromLeft:
Function chewToFromLeft(entry as string, target as string, occurrance as integer) As string
dim loopCounter as integer
dim tempInt1 as integer
dim tempString1 as string
if entry = "" or target = "" or occurrance <1 then
return ""
end if
tempString1 = entry
do
tempInt1 = inStr (tempString1, target)
if tempInt1 = 0 then
return entry
end if
tempString1 = mid (tempString1, (tempInt1 + len (target)))
loopCounter = loopCounter + 1
loop until loopCounter = occurrance
return tempString1
End Function
StringFunctions.chewToFromRight:
Function chewToFromRight(entry as string, target as string, occurrance as integer) As string
dim holdingInt (0) as integer
dim tempInt1 as integer
dim tempString as string
if entry = "" or target = "" or occurrance <1 then
return ""
end if
do // find all occurrances, add them to an array. We'll pick the right one later.
tempInt1 = inStr ((tempInt1 + 1), entry, target)
if tempInt1 = 0 then
exit
else
holdingInt.append tempInt1
end if
loop
if (ubound (holdingInt)) < occurrance then
return entry
else
tempInt1 = ubound (holdingInt) - occurrance + 1
tempInt1 = holdingInt (tempInt1) - 1
tempString = left (entry, tempInt1)
return tempString
end if
End Function
StringFunctions.outStr:
Function outStr(start as integer, base as string, search as string) As integer
dim started as integer
dim tempInt as integer
dim tempInt2 as integer
dim tempInt3 as integer
dim locations (0) as integer
if base = "" or search = "" then
return 0
end if
started = start
if start = 0 then
started = -1 * len (base)
end if
// to start, just find every instance of search
do
tempInt = inStr ((tempInt + 1), base, search)
if tempInt = 0 then
exit
else
locations.append tempInt
end if
loop
if ubound (locations) = 0 then // nothing there
return 0
end if
if (locations (ubound (locations)) + len (search) = (len (base) + 1)) and start = 0 then
return 1
end if
if started < 0 then // we return the distance from the end of the string
tempInt3 = len (base) + started + 1
tempInt = 0
do
tempInt = tempInt + 1
if tempInt > ubound (locations) then
exit
end if
if locations (tempInt) < tempInt3 then
tempInt2 = locations(tempInt)
else
exit
end if
loop
if tempInt = 1 and tempInt2 = 0 then
return 0
end if
tempInt2 = len (base) - tempInt2 + 1
return tempInt2
else // we return the distance from the start of the string
tempInt = 0
do
tempInt = tempInt + 1
if tempInt > ubound (locations) then
exit
end if
if locations (tempInt) < started then
tempInt2 = locations(tempInt)
else
exit
end if
loop
if len (base) < started then
return 0
else
return tempInt2
end if
end if
End Function
StringFunctions.getWord:
Function getWord(entry as string, instance as integer) As string
dim loopCounter as integer
dim spaceSpot as integer
dim returnSpot as integer
dim tabSpot as integer
dim holdSpot as integer
dim tempString as string
dim tempString2 as string
dim holdingString as string
tempString = entry
do
holdSpot = 0
spaceSpot = inStr (tempString, " ")
returnSpot = inStr (tempString, chr (13))
tabSpot = inStr (tempString, chr (9))
if spaceSpot = 0 and returnSpot = 0 and tabSpot = 0 then
if loopCounter + 1 = instance then
return chew (tempString)
else
return ""
end if
end if
if spaceSpot > 1 then
holdSpot = spaceSpot
end if
if returnSpot < holdSpot and returnSpot > 1 then
holdSpot = returnSpot
end if
if holdSpot = 0 and returnSpot >1 then
holdSpot = returnSpot
end if
if tabSpot < holdSpot and tabSpot > 1 then
holdSpot = tabSpot
end if
if holdSpot = 0 and tabSpot >1 then
holdSpot = tabSpot
end if
if holdSpot = 0 then
loopCounter = loopCounter - 1
tempString = mid (tempString, 2)
else
if holdSpot = returnSpot then // check for hyphen!
if mid (tempString, holdSpot - 1, 1) = "-" and chomp (left (tempString, holdSpot)) <> "" then
tempString2 = getWord (mid (tempString, holdSpot), 1)
holdingString = nibble (left (tempString, holdSpot)) + chew (tempString2)
tempString = mid (tempString, (holdSpot + len (tempString2) + 1))
else
holdingString = chomp (left (tempString, holdSpot))
tempString = mid (tempString, holdSpot)
end if
else
holdingString = chomp (left (tempString, holdSpot))
tempString = mid (tempString, (holdSpot + 1))
end if
end if
if hasNonSpace (holdingString) then
loopCounter = loopCounter + 1
end if
loop until loopCounter = instance
return chew (holdingString)
End Function
StringFunctions.splitLeft:
Function splitLeft(entry as string, delimiter as string) As string
if inStr (entry, delimiter) = 0 then
return ""
else
return left (entry, (inStr (entry, delimiter) - 1))
end if
End Function
StringFunctions.splitRight:
Function splitRight(entry as string, delimiter as string) As string
if inStr (entry, delimiter) = 0 then
return ""
else
return mid (entry, (inStr (entry, delimiter) + len (delimiter)))
end if
End Function
StringFunctions.csInStr:
Function csInStr(start as integer, base as string, query as string) As integer
dim tempString as string
dim tempInt1 as integer
dim tempInt2 as integer
dim test as integer
tempInt1 = start
do
tempInt2 = inStr(tempInt1, base, query)
if tempInt2 = 0 then
return 0
else
tempString = mid (base, tempInt2, len (query))
test = strComp (query, tempString, 0)
if test = 0 then
return tempInt2
else
tempInt1 = tempInt2 + 1
end if
end if
loop
End Function
StringFunctions.csSplitLeft:
Function csSplitLeft(base as string, target as string) As string
if csInStr (1, base, target) = 0 then
return ""
else
return left (base, csInStr (1, base, target))
end if
End Function
StringFunctions.csSplitRight:
Function csSplitRight(base as string, target as string) As string
if csInStr (1, base, target) = 0 then
return ""
else
return mid (base, (csInStr (1, base, target) + len (target)))
end if
End Function
StringFunctions.csReplaceAll:
Function csReplaceAll(base as string, target as string, replacement as string) As string
dim tempString as string
tempString = base
do
if csInStr (1, tempString, target) = 0 then
return tempString
else
tempString = csSplitLeft (tempString, target) + replacement + csSplitRight (tempString, target)
end if
loop
End Function
bracketedObject.findAll:
Function findAll(leftBracket as string, rightBracket as string, entry as string) As Boolean
dim tempInt1 as integer
dim tempInt2 as integer
dim counter as integer
dim tempString1 as string
dim holdingString as string
if leftBracket = "" or rightBracket = "" or entry = "" then
redim result (0)
result (0) = ""
totalFound = 0
anyFound = false
return false
end if
tempString1 = entry
if leftBracket = rightBracket then
do
tempInt1 = inStr (tempString1, rightBracket)
tempInt2 = inStr (tempInt1 + 1, tempString1, rightBracket)
if tempInt2 = 0 then
exit // get out of here if you can't find any instances of the bracket
end if
if tempInt2 = (tempInt1 +1) then // they're right on top of each other, get rid of the first
tempString1 = mid (tempString1, tempInt2)
holdingString = ""
else // there's something there, but is it just a space?
holdingString = mid (tempString1, (tempInt1+1), (tempInt2 - tempInt1 - 1))
if hasNonSpace (holdingString) then // we're okay
tempString1 = mid (tempString1, tempInt2 )
else
tempString1 = mid (tempString1, tempInt2)
holdingString = ""
end if
end if
if holdingString <> "" then
counter = counter + 1
if counter = 1 then
result (0) = holdingString
else
result.append holdingString
end if
end if
loop
else // different brackets!
do
tempInt1 = inStr (tempString1, rightBracket)
if tempInt1 = 0 then
exit
end if
if tempInt1 < 2 then // ignore if it's the first thing there
tempString1 = mid (tempString1, (tempInt1 + 1))
end if
holdingString = left (tempString1, (tempInt1-1))
if inStr (holdingString, leftBracket) = 0 then // see if leftBracket's there
// if not, chop down the string and don't increment the loop
tempString1 = mid (tempString1, (tempInt1 + 1))
else
holdingString = chompToFromLeft (holdingString, leftBracket)
if hasNonSpace (holdingString) then
tempString1 = mid (tempString1, (tempInt1 + 1))
else // the remaining bit is all spaces, so ignore
tempString1 = mid (tempString1, (tempInt1 + 1))
holdingString = ""
end if
end if
if holdingString <> "" then
counter = counter + 1
if counter = 1 then
result (0) = holdingString
else
result.append holdingString
end if
end if
loop
end if
if result (0) = "" then
anyFound = false
totalFound = 0
return false
else
anyFound = true
totalFound = ubound (result) + 1
return true
end if
End Function
wordObject.findAll:
Function findAll(entry as string) As boolean
dim loopCounter as integer
dim spaceSpot as integer
dim returnSpot as integer
dim tabSpot as integer
dim holdSpot as integer
dim tempString as string
dim tempString2 as string
dim holdingString as string
tempString = entry
if tempString = "" then
totalFound = 0
anyFound = false
redim result (0)
result (0) = ""
return false
end if
do
holdSpot = 0
spaceSpot = inStr (tempString, " ")
returnSpot = inStr (tempString, chr (13))
tabSpot = inStr (tempString, chr (9))
if spaceSpot = 0 and returnSpot = 0 and tabSpot = 0 then
if chew (tempString) <> "" then
result.append chew (tempString)
exit
else
exit
end if
end if
if spaceSpot > 1 then
holdSpot = spaceSpot
end if
if returnSpot < holdSpot and returnSpot > 1 then
holdSpot = returnSpot
end if
if holdSpot = 0 and returnSpot >1 then
holdSpot = returnSpot
end if
if tabSpot < holdSpot and tabSpot > 1 then
holdSpot = tabSpot
end if
if holdSpot = 0 and tabSpot >1 then
holdSpot = tabSpot
end if
if holdSpot = 0 then
loopCounter = loopCounter - 1
tempString = mid (tempString, 2)
else
if holdSpot = returnSpot then // check for hyphen!
if mid (tempString, holdSpot - 1, 1) = "-" and chomp (left (tempString, holdSpot)) <> "" then
tempString2 = getWord (mid (tempString, holdSpot), 1)
holdingString = nibble (left (tempString, holdSpot)) + chew (tempString2)
tempString = mid (tempString, (holdSpot + len (tempString2) + 2))
else
holdingString = chomp (left (tempString, holdSpot))
tempString = mid (tempString, holdSpot)
end if
else
holdingString = chomp (left (tempString, holdSpot))
tempString = mid (tempString, (holdSpot + 1))
end if
end if
if hasNonSpace (chew (holdingString)) then
loopCounter = loopCounter + 1
if loopCounter = 1 then
result (0) = chew (holdingString)
else
result.append chew (holdingString)
end if
end if
loop
if result (0) <> "" then
totalFound = ubound (result) + 1
anyFound = true
return true
else
return false
end if
End Function
splitObject.split:
Function split(entry as string, delimiter as string) As Boolean
if inStr (entry, delimiter) = 0 then
left = ""
right = ""
return false
else
left = splitLeft (entry, delimiter)
right = splitRight (entry, delimiter)
return true
end if
End Function
parseObject.parse:
Function parse(entry as string, delimiter as string) As boolean
dim tempString as string
if inStr (entry, delimiter) = 0 then
anyPresent = false
numberPresent = 0
redim result (0)
result (0) = ""
return false
end if
result (0) = splitLeft (entry, delimiter)
tempString = splitRight (entry, delimiter)
do
result.append splitLeft (tempString, delimiter)
if result (ubound (result)) = "" then
result (ubound (result)) = tempString
exit
end if
tempString = splitRight (tempString, delimiter)
loop
anyPresent = true
numberPresent = ubound (result)
return true
End Function